<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of forum_model
 *
 * @author aquilax
 */
class Forum_model extends Model{

  public $forum_id = 0;
  public $user_id = 0;

  function getForumsAndGroups($forum_id, $user_id){
    $select = array(
      'g.id AS group_id',
      'g.name AS group_name',
      'c.id AS category_id',
      'c.slug AS category_slug',
      'c.name AS category_name'
    );
    $this->db->select($select);
    $this->db->where('g.forum_id', $forum_id);
    $this->db->where('c.forum_id', $forum_id);
    $this->db->join('category c', 'c.group_id = g.id');
    $this->db->order_by('g.sorder');
    $this->db->order_by('g.created');
    $this->db->order_by('c.sorder');
    $this->db->order_by('c.created');

    //just for prevention
    $limit = 100;
    
    $query = $this->db->get('group g', $limit);
    return $query->result_array();
  }

  function loadCategory($category_id){
    $select = array(
      'g.id AS group_id',
      'g.name AS group_name',
      'c.id AS category_id',
      'c.slug AS category_slug',
      'c.name AS category_name'
    );
    $this->db->select($select);
    $this->db->where('c.forum_id', $this->forum_id);
    $this->db->where('c.id', $category_id);
    $this->db->join('group g', 'g.id = c.group_id');
    $query = $this->db->get('category c', 1);
    return $query->row_array();
  }

  function getTopicsForCategory($forum_id, $user_id, $category_id, $limit, $offset){
    $select = array(
      'p.id AS post_id',
      'p.category_id',
      'p.user_id',
      'u2.id AS last_user_id',
      'u.username',
      'u2.username AS last_username',
      'p.title',
      'p.slug as post_slug',
      'p.replies',
      'p.views',
      'p.last_created',
      'p.sorder',
      'UNIX_TIMESTAMP(p.last_created) AS created',
    );
    $this->db->select($select);
    $this->db->where('p.parent_id', 0);
    $this->db->where('p.forum_id', $forum_id);
    $this->db->where('p.category_id', $category_id);
    $this->db->join('user u', 'u.id = p.user_id');
    $this->db->join('user u2', 'u2.id = p.last_user_id', 'LEFT');
    $this->db->order_by('p.sorder', 'DESC');
    $this->db->order_by('p.last_created', 'DESC');
    $this->db->order_by('p.created', 'DESC');
    $this->db->limit($limit, $offset);
    $query = $this->db->get('post p');
    return $query->result_array();
  }

  function getTopicsCountForCategory($category_id){
    $this->db->where('parent_id', 0);
    $this->db->where('forum_id', $this->forum_id);
    $this->db->where('category_id', $category_id);
    $this->db->from('post');
    return $this->db->count_all_results();
  }

  function getTopic($post_id){
    $select = array(
      'p.id AS post_id',
      'p.parent_id',
      'p.title AS title',
      'p.slug AS post_slug',
      'p.body_r AS body_r',
      'c.id as category_id',
      'c.slug as category_slug',
      'c.name as category_name',
      'UNIX_TIMESTAMP(p.created) AS created'
    );
    $this->db->select($select);
    $this->db->where('p.forum_id', $this->forum_id);
    $this->db->where('p.id', $post_id);
    $this->db->join('category c', 'c.id = p.category_id');
    $this->db->order_by('parent_id');
    $this->db->order_by('p.created');
    $query = $this->db->get('post p', 1);
    return $query->row_array();
  }

  function getPosts($post_id, $limit, $offset){
    $select = array(
      'p.id AS post_id',
      'p.parent_id',
      'p.title AS title',
      'p.slug AS post_slug',
      'p.body_r AS body_r',
      'u.id AS user_id',
      'u.username',
      'u.email',
      'c.id as category_id',
      'c.slug as category_slug',
      'c.name as category_name',
      'UNIX_TIMESTAMP(p.created) AS created'
    );
    $this->db->select($select);
    $this->db->where('p.forum_id', $this->forum_id);
    $this->db->where('p.id', $post_id);
    $this->db->or_where('p.parent_id', $post_id);
    $this->db->join('user u', 'u.id = p.user_id');
    $this->db->join('category c', 'c.id = p.category_id');
    $this->db->order_by('parent_id');
    $this->db->order_by('p.created');
    $this->db->limit($limit, $offset);
    $query = $this->db->get('post p');
    return $query->result_array();
  }

  function getPostCountForTopic($post_id){
    $this->db->where('p.forum_id', $this->forum_id);
    $this->db->where('p.id', $post_id);
    $this->db->or_where('p.parent_id', $post_id);
    $this->db->from('post p');
    return $this->db->count_all_results();
  }

  function _loadPost($post_id){
    $this->db->where('id', $post_id);
    $this->db->where('forum_id', $this->forum_id);
    $query = $this->db->get('post p', 1);
    return $query->row_array();
  }

  function _createSlug($text){
    $bg = array( 'А','Б','В','Г','Д','Е','Ж','З','И','Й',
                 'К','Л','М','Н','О','П','Р','С','Т','У',
                 'Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь','Ю','Я',
                 'а','б','в','г','д','е','ж','з','и','й',
                 'к','л','м','н','о','п','р','с','т','у',
                 'ф','х','ц','ч','ш','щ','ъ','ь','ю','я', ' ',
                 '0','1','2','3','4','5','6','7','8','9');
    $en = array( 'A','B','V','G','D','E','J','Z','I','Y',
                 'K','L','M','N','O','P','R','S','T','U',
                 'F','H','C','CH','SH','SHT','Y','Y','YU','YA',
                 'a','b','v','g','d','e','j','z','i','y',
                 'k','l','m','n','o','p','r','s','t','u',
                 'f','h','c','ch','sh','sht','y','y','yu','ya', '_',
                 '0','1','2','3','4','5','6','7','8','9');
    $str =  strtolower(str_replace($bg, $en, $text));
		$str1 = '';
    for($a=0; $a < strlen($str); $a++){
      $c = ord(substr($str,$a,1));
      if (($c >= 48) && ($c <= 57) or
         ($c >= 97) &&  ($c <= 122) or
         ($c == 95)){
        $str1 .= substr($str,$a,1);
      }
    }
    return $str1;
  }

  function _increaseReplies($post_id, $new_id){
    $this->db->where('id', $post_id);
    $this->db->where('forum_id', $this->forum_id);
    $this->db->set('replies', 'replies+1', FALSE);
    $this->db->set('last_id', $new_id);
    $this->db->set('last_created', 'NOW()', FALSE);
    $this->db->set('last_user_id', $this->user_id);
    $this->db->update('post');
  }

  function post($parent_id, $category_id, $title, $body){
    $post = FALSE;
    $number = 1;
    if ($parent_id){
      $post = $this->_loadPost($parent_id);
      if (!$post){
        return FALSE;
      }
      $number = ($post['replies']) + 1;
      $category_id = $post['category_id'];
    }
    $this->load->library('BBCode');
    $this->bbcode->SetSmileyURL(site_url('/img/smileys/'));
    $data = array(
      'parent_id' => $parent_id,
      'forum_id' => $this->forum_id,
      'category_id' => $category_id,
      'user_id' => $this->user_id,
      'title' => $title,
      'body' => $body,
      'number' => $number,
      'body_r' => $this->bbcode->Parse($body),
      'slug' => $this->_createSlug($title),
      'last_user_id' => $this->user_id
    );
    $this->db->set('last_created', 'NOW()', FALSE);
    $this->db->insert('post', $data);
    $new_id = $this->db->insert_id();
    if ($post){
      $this->_increaseReplies($post['id'], $new_id);
      return array($new_id, $post['slug'], $number);
    } else {
      return array($new_id, $data['slug'], $number);
    }
  }

}
?>